var eventsMgr = require('eventsMgr');
var consts = require('consts');
var actions = require('actions');

var CellNode = require('CellNode');
cc.Class({
    extends: cc.Component,
    properties: {
        capacity: {
            // ATTRIBUTES:
            default:1,        // The default value will be used only when the component attaching
            type:cc.Integer,
            serializable: true,   // optional, default is true
            tooltip:'允许放入拖拽节点的数量',
        },
        groups: {
            // ATTRIBUTES:
            default:[],        // The default value will be used only when the component attaching
            type:cc.String,
            serializable: true,   // optional, default is true
            tooltip:'允许放入拖拽节点的组',
        },
        nodes:{
            default:[],        // The default value will be used only when the component attaching
            type:cc.Node,
            serializable: true,   // optional, default is true
            tooltip:'被放到容器内的节点',
        },
    },

    // LIFE-CYCLE CALLBACKS:
    onLoad () {
        let self = this;
        if (self.node.width === 0 || self.node.height === 0){
            cc.warn('容器没有宽高<'+self.node.name+'>');
        }
        self.curCount = 0;
        if (self.capacity <= 0){
            self.capacity = 1;
        }
    },
    start () {

    },
    isFull(){

    },
    addNode(node){
        let self = this;
        let isAddOk = false;
        if (self.curCount < self.capacity){
            let cellNode = node.getComponent(CellNode);
            let index = self.groups.indexOf(cellNode.groupId);
            if (index >= 0){//是被允许的分组
                isAddOk = true;
                cellNode.isMoveEnable = false;
                self.nodes.push(node);
                // actions.moveTo(self.node,0.2,self.node.x,self.node.y,function () {
                //     node.x = 0;
                //     node.y = 0;
                // });
                node.x -= self.node.x;
                node.y -= self.node.y;
                node.parent = self.node;
                self.curCount++;
            }else{
                actions.shake(self.node,0.5);
                cc.log('当前容器不允许放入');
            }
        }else{
            // actions.shake(self.node,0.5);
            cc.log('当前容器已经满了');
        }
        return isAddOk;
    },

});
